<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body>
<div class="article-body">
    <div class="article-intro">

        <h1>Java  Scanner 类</h1>
        <p>java.util.Scanner 是 Java5 的新特征，我们可以通过 Scanner 类来获取用户的输入。
        </p><p>下面是创建 Scanner 对象的基本语法：</p>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-identifier">Scanner</span><span class="hl-code"> </span><span class="hl-identifier">s</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Scanner</span><span class="hl-brackets">(</span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">in</span><span class="hl-brackets">)</span><span class="hl-code">;</span></pre>
            </div>
        </div>
        <p>接下来我们演示一个最简单的数据输入，并通过 Scanner 类的 next() 与 nextLine() 方法获取输入的字符串，在读取前我们一般需要
            使用 hasNext 与 hasNextLine 判断是否还有输入的数据：</p>
        <p></p>
        <h3>使用 next 方法：</h3>
        <div class="example">
            <h2 class="example">ScannerDemo.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">util</span><span class="hl-code">.</span><span class="hl-identifier">Scanner</span><span class="hl-code">;

</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">ScannerDemo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">Scanner</span><span class="hl-code"> </span><span class="hl-identifier">scan</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Scanner</span><span class="hl-brackets">(</span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">in</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-comment">//</span><span class="hl-comment"> 从键盘接收数据</span><span class="hl-comment"></span><span class="hl-code">

        </span><span class="hl-comment">//</span><span class="hl-comment"> next方式接收字符串</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">next方式接收：</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-comment">//</span><span class="hl-comment"> 判断是否还有输入</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">scan</span><span class="hl-code">.</span><span class="hl-identifier">hasNext</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
            </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">str1</span><span class="hl-code"> = </span><span class="hl-identifier">scan</span><span class="hl-code">.</span><span class="hl-identifier">next</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
            </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">输入的数据为：</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">str1</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-brackets">}</span><span class="hl-code">
        </span><span class="hl-identifier">scan</span><span class="hl-code">.</span><span class="hl-identifier">close</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>执行以上程序输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ javac </span><span class="typ">ScannerDemo</span><span class="pun">.</span><span class="pln">java
$ java </span><span class="typ">ScannerDemo</span><span class="pln">
</span><span class="kwd">next</span><span class="pun">方式接收：</span><span class="pln">
runoob com
</span><span class="pun">输入的数据为：</span><span class="pln">runoob</span></pre>
        <p>可以看到 com 字符串并未输出，接下来我们看 nextLine。</p>
        <h3>使用 nextLine 方法：</h3>
        <div class="example">
            <h2 class="example">ScannerDemo.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">util</span><span class="hl-code">.</span><span class="hl-identifier">Scanner</span><span class="hl-code">;

</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">ScannerDemo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">Scanner</span><span class="hl-code"> </span><span class="hl-identifier">scan</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Scanner</span><span class="hl-brackets">(</span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">in</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-comment">//</span><span class="hl-comment"> 从键盘接收数据</span><span class="hl-comment"></span><span class="hl-code">

        </span><span class="hl-comment">//</span><span class="hl-comment"> nextLine方式接收字符串</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">nextLine方式接收：</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-comment">//</span><span class="hl-comment"> 判断是否还有输入</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">scan</span><span class="hl-code">.</span><span class="hl-identifier">hasNextLine</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
            </span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">str2</span><span class="hl-code"> = </span><span class="hl-identifier">scan</span><span class="hl-code">.</span><span class="hl-identifier">nextLine</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
            </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">输入的数据为：</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">str2</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-brackets">}</span><span class="hl-code">
        </span><span class="hl-identifier">scan</span><span class="hl-code">.</span><span class="hl-identifier">close</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>执行以上程序输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ javac </span><span class="typ">ScannerDemo</span><span class="pun">.</span><span class="pln">java
$ java </span><span class="typ">ScannerDemo</span><span class="pln">
nextLine</span><span class="pun">方式接收：</span><span class="pln">
runoob com
</span><span class="pun">输入的数据为：</span><span class="pln">runoob com</span></pre>
        <p>可以看到 com 字符串输出。</p>
        <h3>next() 与 nextLine() 区别</h3>
        <p>next():</p><ul><li>
        1、一定要读取到有效字符后才可以结束输入。</li><li>
        2、对输入有效字符之前遇到的空白，next() 方法会自动将其去掉。
    </li><li>
        3、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。 </li><li>
        next() 不能得到带有空格的字符串。</li></ul>
        <p>nextLine()： </p><ul><li>
        1、以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。 </li><li>
        2、可以获得空白。</li></ul>

        <p>如果要输入 int 或 float 类型的数据，在 Scanner 类中也有支持，但是在输入之前最好先使用 hasNextXxx() 方法进行验证，再使用 nextXxx() 来读取：</p>
        <div class="example">
            <h2 class="example">ScannerDemo.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">util</span><span class="hl-code">.</span><span class="hl-identifier">Scanner</span><span class="hl-code">;

</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">ScannerDemo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">Scanner</span><span class="hl-code"> </span><span class="hl-identifier">scan</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Scanner</span><span class="hl-brackets">(</span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">in</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-comment">//</span><span class="hl-comment"> 从键盘接收数据</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">;
        </span><span class="hl-types">float</span><span class="hl-code"> </span><span class="hl-identifier">f</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-number">.0</span><span class="hl-identifier">f</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">输入整数：</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">scan</span><span class="hl-code">.</span><span class="hl-identifier">hasNextInt</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
            </span><span class="hl-comment">//</span><span class="hl-comment"> 判断输入的是否是整数</span><span class="hl-comment"></span><span class="hl-code">
            </span><span class="hl-identifier">i</span><span class="hl-code"> = </span><span class="hl-identifier">scan</span><span class="hl-code">.</span><span class="hl-identifier">nextInt</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
            </span><span class="hl-comment">//</span><span class="hl-comment"> 接收整数</span><span class="hl-comment"></span><span class="hl-code">
            </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">整数数据：</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">i</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">else</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
            </span><span class="hl-comment">//</span><span class="hl-comment"> 输入错误的信息</span><span class="hl-comment"></span><span class="hl-code">
            </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">输入的不是整数！</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-brackets">}</span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">输入小数：</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">scan</span><span class="hl-code">.</span><span class="hl-identifier">hasNextFloat</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
            </span><span class="hl-comment">//</span><span class="hl-comment"> 判断输入的是否是小数</span><span class="hl-comment"></span><span class="hl-code">
            </span><span class="hl-identifier">f</span><span class="hl-code"> = </span><span class="hl-identifier">scan</span><span class="hl-code">.</span><span class="hl-identifier">nextFloat</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
            </span><span class="hl-comment">//</span><span class="hl-comment"> 接收小数</span><span class="hl-comment"></span><span class="hl-code">
            </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">小数数据：</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">f</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-brackets">}</span><span class="hl-code"> </span><span class="hl-reserved">else</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
            </span><span class="hl-comment">//</span><span class="hl-comment"> 输入错误的信息</span><span class="hl-comment"></span><span class="hl-code">
            </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">输入的不是小数！</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-brackets">}</span><span class="hl-code">
        </span><span class="hl-identifier">scan</span><span class="hl-code">.</span><span class="hl-identifier">close</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>执行以上程序输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ javac </span><span class="typ">ScannerDemo</span><span class="pun">.</span><span class="pln">java
$ java </span><span class="typ">ScannerDemo</span><span class="pln">
</span><span class="pun">输入整数：</span><span class="lit">12</span><span class="pln">
</span><span class="pun">整数数据：</span><span class="lit">12</span><span class="pln">
</span><span class="pun">输入小数：</span><span class="lit">1.2</span><span class="pln">
</span><span class="pun">小数数据：</span><span class="lit">1.2</span></pre>

        <p>以下实例我们可以输入多个数字，并求其总和与平均数，每输入一个数字用回车确认，通过输入非数字来结束输入并输出执行结果：</p>
        <div class="example">
            <h2 class="example">ScannerDemo.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">import</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">util</span><span class="hl-code">.</span><span class="hl-identifier">Scanner</span><span class="hl-code">;

</span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">ScannerDemo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">Scanner</span><span class="hl-code"> </span><span class="hl-identifier">scan</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Scanner</span><span class="hl-brackets">(</span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">in</span><span class="hl-brackets">)</span><span class="hl-code">;

        </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">sum</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">;
        </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">m</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">;

        </span><span class="hl-reserved">while</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">scan</span><span class="hl-code">.</span><span class="hl-identifier">hasNextDouble</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
            </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span class="hl-code"> = </span><span class="hl-identifier">scan</span><span class="hl-code">.</span><span class="hl-identifier">nextDouble</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
            </span><span class="hl-identifier">m</span><span class="hl-code"> = </span><span class="hl-identifier">m</span><span class="hl-code"> + </span><span class="hl-number">1</span><span class="hl-code">;
            </span><span class="hl-identifier">sum</span><span class="hl-code"> = </span><span class="hl-identifier">sum</span><span class="hl-code"> + </span><span class="hl-identifier">x</span><span class="hl-code">;
        </span><span class="hl-brackets">}</span><span class="hl-code">

        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-identifier">m</span><span class="hl-code"> + </span><span class="hl-quotes">"</span><span class="hl-string">个数的和为</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">sum</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-identifier">m</span><span class="hl-code"> + </span><span class="hl-quotes">"</span><span class="hl-string">个数的平均值是</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-brackets">(</span><span class="hl-identifier">sum</span><span class="hl-code"> / </span><span class="hl-identifier">m</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">scan</span><span class="hl-code">.</span><span class="hl-identifier">close</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>执行以上程序输出结果为：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ javac </span><span class="typ">ScannerDemo</span><span class="pun">.</span><span class="pln">java
$ java </span><span class="typ">ScannerDemo</span><span class="pln">
</span><span class="lit">12</span><span class="pln">
</span><span class="lit">23</span><span class="pln">
</span><span class="lit">15</span><span class="pln">
</span><span class="lit">21.4</span><span class="pln">
</span><span class="kwd">end</span><span class="pln">
</span><span class="lit">4</span><span class="pun">个数的和为</span><span class="lit">71.4</span><span class="pln">
</span><span class="lit">4</span><span class="pun">个数的平均值是</span><span class="lit">17.85</span></pre>


        <p>更多内容可以参考 API 文档：<a href="http://www.runoob.com/manual/jdk1.6/" target="_blank">http://www.runoob.com/manual/jdk1.6/</a>。</p>

    </div>
</div>
</body>
</html>
